<template>
  <div id="Content-page">
    <Paging
      :pagData="[
        { name: '推荐游记', router: '/reco-travel' },
        { name: '选择游记', router: '/reco-travel-upload' },
      ]"
    />
    <div class="page-hero">
      <h2 class="hero-title">推荐游记封面</h2>
      <p class="hero-sub">挑选并上传一张具有旅行氛围的封面图，让内容更吸睛</p>
    </div>
    <div class="content-main">
      <div class="global-display global-j-content">
        <el-upload
          ref="uploadEle"
          :action="IMAGE_URL"
          accept=".jpg,.png,.jpeg,.webp"
          :limit="1"
          :show-file-list="false"
          :on-success="onSuccess"
          :on-error="onError"
        >
          <div class="left-upload left-upload-icon">
            <div class="left-upload-icon" v-if="uploadImgurl == ''">
              <el-icon :size="20"><Plus /></el-icon>
              <span>建议上传大小不超过500kb，宽为280px，高为200px</span>
            </div>
            <div v-else>
              <img :src="uploadImgurl" alt="" />
            </div>
          </div>
        </el-upload>
        <div class="right-upload-button">
          <el-button type="success" class="submit-button" :loading="loadIng" @click="subMit">
            {{ recoTravelId == '' ? '提交' : '确认修改' }}
          </el-button>
        </div>
      </div>
      <!-- 已选择游记 -->
      <div v-if="tableSelect.length > 0">
        <p class="select-Title">已选择游记</p>
        <el-table :data="tableSelect" style="width: 100%">
          <el-table-column prop="author_data[0].nickname" label="作者" width="180" />
          <el-table-column prop="time" label="发布时间" />
          <el-table-column prop="address" label="位置" />
          <el-table-column prop="title" label="标题" />
        </el-table>
      </div>
      <!-- 选择游记 -->
      <p class="select-Title">请选择一个游记关联</p>
      <el-table
        :data="userTarvelData"
        highlight-current-row
        style="width: 100%"
        @current-change="handleCurrentChange"
      >
        <el-table-column prop="author_data[0].nickname" label="作者" width="180" />
        <el-table-column prop="time" label="发布时间" />
        <el-table-column prop="address" label="位置" />
        <el-table-column prop="title" label="标题" />
      </el-table>
      <!-- 分页 -->
      <div class="pager-next">
        <el-pagination
          background
          layout="prev, pager, next, total"
          :total="count"
          @current-change="currentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
// 顶部导航组件
import Paging from '@/page/components/Paging-component.vue'
import { Plus } from '@element-plus/icons-vue'
import { ref, onMounted } from 'vue'
import { getAllUserTravelAPI, submitRecomTravelAPI, modifyRecomTravelAPI } from '@/api/reco-travel'
import type { GetAllUserTravelItem } from '@/type/reco-travel'
import type { UploadImageResponse } from '@/type/global'
import type { UploadFile, UploadFiles } from 'element-plus'

import { useRouter, useRoute } from 'vue-router'
const $router = useRouter() // 这是路由跳转的
const $routeQuery = useRoute() // 用于接收路由参数的
//#region
// 存储上传的图片地址
const loadIng = ref(false)
// 未上传和已上传的图片展示
const uploadImgurl = ref('')
const uploadEle = ref()
const IMAGE_URL = ref('')
// 上传成功
function onSuccess(response: UploadImageResponse) {
  uploadImgurl.value = response.data[0]
  uploadEle.value.clearFiles()
}
// 上传失败
function onError(error: Error, uploadFile: UploadFile, uploadFiles: UploadFiles) {
  console.log(error, uploadFile, uploadFiles)
  ElMessage.error('上传图片失败')
  ElMessage('上传图片失败')
}
//#endregion
// 已选择的游记
const tableSelect = ref<GetAllUserTravelItem[]>([])
const travelId = ref('') //游记_id
const recoTravelId = ref('') //该条数据_id
onMounted(async () => {
  IMAGE_URL.value = import.meta.env.VITE_API_IMAGE_URL!
  if (JSON.stringify($routeQuery.query) != '{}') {
    const { imageUrl, _id } = $routeQuery.query
    uploadImgurl.value = imageUrl as string
    recoTravelId.value = _id as string
  }
  await getAllUserTravelData()
})
const count = ref(1)
const userTarvelData = ref<GetAllUserTravelItem[]>([])
async function getAllUserTravelData(page = 1) {
  const res = await getAllUserTravelAPI(page)
  console.log(res)
  userTarvelData.value = res.data.data
  count.value = res.data.count
}
// 分页
async function currentChange(event: number) {
  await getAllUserTravelData(event)
}
// 选择游记
function handleCurrentChange(event: GetAllUserTravelItem) {
  console.log(event)

  tableSelect.value = event ? [event] : tableSelect.value
  travelId.value = event ? event._id : travelId.value
}
// 提交
async function subMit() {
  try {
    loadIng.value = true
    if (recoTravelId.value == '') {
      const res = await submitRecomTravelAPI({
        imageUrl: uploadImgurl.value,
        travel_id: travelId.value,
      })
      if (res.msg !== 'SUCCESS') {
        ElMessage.error(res.msg)
      }
      ElMessage.success(res.data.message || '提交成功')
    } else {
      const res = await modifyRecomTravelAPI({
        _id: recoTravelId.value,
        imageUrl: uploadImgurl.value,
        travel_id: travelId.value,
      })
      if (res.msg !== 'SUCCESS') {
        ElMessage.error(res.msg)
      }
      ElMessage.success(res.data.message || '修改成功')
    }
    $router.push('/reco-travel')
  } catch (error) {
    ElMessage.error(error instanceof Error ? error.message : '提交失败')
  } finally {
    loadIng.value = false
  }
}
</script>

<style scoped>
#Content-page {
  --travel-primary: #0bb5a7;
  --travel-primary-dark: #089d91;
  --travel-accent: #ffc857;
  --travel-sky: #e9f9f7;
  --travel-sea: #d6f0ee;
  --text-color: #1f2d3d;
}

.content-main {
  background: linear-gradient(180deg, var(--travel-sky) 0%, #fff 60%);
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.06);
  padding: 24px;
  margin-top: 16px;
}

/* 上传区域 */
.global-display.global-j-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 16px;
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.05);
  border: 1px solid rgba(11, 181, 167, 0.08);
}

.left-upload {
  border: 2px dashed rgba(11, 181, 167, 0.35);
  color: rgba(0, 0, 0, 0.45);
  width: 280px;
  height: 200px;
  border-radius: 12px;
  overflow: hidden;
  background: linear-gradient(135deg, var(--travel-sea) 0%, #fff 100%);
  transition: all 0.25s ease;
}

.left-upload:hover {
  border-color: var(--travel-primary);
  box-shadow: 0 10px 20px rgba(11, 181, 167, 0.15);
  transform: translateY(-1px);
}

.left-upload-icon {
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
  justify-content: center;
  color: #6b7280;
}

.left-upload .el-icon {
  color: var(--travel-primary);
}

.left-upload img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.right-upload-button {
  display: flex;
  align-items: center;
  justify-content: flex-start;
}

.submit-button {
  background: linear-gradient(135deg, var(--travel-primary) 0%, var(--travel-primary-dark) 100%);
  border: none;
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  box-shadow: 0 8px 16px rgba(11, 181, 167, 0.25);
  transition:
    transform 0.15s ease,
    box-shadow 0.15s ease;
}

.submit-button:hover {
  transform: translateY(-1px);
  box-shadow: 0 12px 22px rgba(11, 181, 167, 0.28);
}

.select-Title {
  font-size: 18px;
  font-weight: 700;
  margin-top: 28px;
  margin-bottom: 12px;
  color: var(--text-color);
  position: relative;
  display: inline-block;
  padding-left: 10px;
}

.select-Title::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  width: 4px;
  height: 70%;
  transform: translateY(-50%);
  border-radius: 2px;
  background: linear-gradient(180deg, var(--travel-primary), var(--travel-accent));
}

/* 表格美化 */
:deep(.el-table) {
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.05);
  border: 1px solid rgba(11, 181, 167, 0.08);
}

:deep(.el-table__header th) {
  background-color: var(--travel-sky) !important;
  color: #334155;
  font-weight: 600;
}

:deep(.el-table__row:hover) > td {
  background-color: #f5fbfa !important;
}

.pager-next {
  display: flex;
  justify-content: center;
  margin-top: 16px;
}

:deep(.el-pagination.is-background .el-pager li.is-active) {
  background-color: var(--travel-primary);
}

:deep(.el-pagination.is-background .el-pager li:hover) {
  color: var(--travel-primary);
}

/* 顶部横幅 */
.page-hero {
  position: relative;
  background: linear-gradient(135deg, #c8f5f0 0%, #f8fff5 100%);
  border: 1px solid rgba(11, 181, 167, 0.08);
  border-radius: 16px;
  padding: 18px 20px;
  margin-top: 12px;
  overflow: hidden;
}

.page-hero::after {
  content: '';
  position: absolute;
  right: -40px;
  top: -40px;
  width: 140px;
  height: 140px;
  border-radius: 50%;
  background: radial-gradient(circle at 30% 30%, rgba(11, 181, 167, 0.25), transparent 60%);
}

.hero-title {
  margin: 0;
  font-size: 20px;
  color: #0f172a;
}

.hero-sub {
  margin: 6px 0 0;
  color: #475569;
  font-size: 13px;
}
@media (max-width: 768px) {
  .global-display.global-j-content {
    flex-direction: column;
  }
}
</style>
